﻿using System;
using System.Collections.Generic;
using System.Text;

/*
 * http://www.codeproject.com/KB/windows/celocalization.aspx
 */

namespace Close.Classes
{

    /// <summary>
    /// Provides methos to get and set cultures.
    /// (Workaround for windows mobile 6.0)
    /// </summary>
    public static class MobileGlobalization
    {
        /// <summary>
        /// Occurs, when the CurrentCulture is changed.
        /// </summary>
        public static event System.EventHandler CurrentCultureChanged;
        /// <summary>
        /// Occurs, when the CurrentUICulture is changed.
        /// </summary>
        public static event System.EventHandler CurrentUICultureChanged;

        /// <summary>
        /// Gets or stes the System.Globalization.CultureInfo
        /// that represents the culture used by the current thread.
        /// </summary>
        public static System.Globalization.CultureInfo CurrentCulture
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentCulture;
            }
            set
            {
                if (value.Name != System.Globalization.CultureInfo.CurrentUICulture.Name)
                {
                    MobileGlobalization.SetCultureInfo("m_userDefaultCulture", value);
                    OnCurrentCultureChanged(new System.EventArgs());
                }
            }
        }
        /// <summary>
        /// Gets or stes the System.Globalization.CultureInfo
        /// that represents the culture used by the Resouce Manager 
        /// to lookup culture-specific resources at run time.
        /// </summary>
        public static System.Globalization.CultureInfo CurrentUICulture
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentUICulture;
            }
            set
            {
                if (value.Name != System.Globalization.CultureInfo.CurrentUICulture.Name)
                {
                    MobileGlobalization.SetCultureInfo("m_userDefaultUICulture", value);
                    OnCurrentUICultureChanged(new System.EventArgs());
                }
            }
        }
        /// <summary>
        /// Sets the specified cultureInfo to the field
        /// </summary>
        /// <param name="fieldName">Name of the field to set 
        /// the specified cultureInfo.</param>
        /// <param name="cultureInfo">System.Globalization.CultureInfo 
        /// to set to the field for specified fieldName.</param>
        private static void SetCultureInfo(string fieldName, System.Globalization.CultureInfo cultureInfo)
        {
            System.Reflection.FieldInfo currentCulture =
typeof(System.Globalization.CultureInfo).GetField(fieldName,
                                                           System.Reflection.BindingFlags.Static
|
                                                           System.Reflection.BindingFlags.NonPublic);

            currentCulture.SetValue(null, cultureInfo);
        }
        /// <summary>
        /// Raises the CurrentCultureChanged event.
        /// </summary>
        private static void OnCurrentCultureChanged(System.EventArgs e)
        {
            if (CurrentCultureChanged != null)
            {
                CurrentCultureChanged(null, e);
            }
        }
        /// <summary>
        /// Raises the CurrentUICultureChanged event.
        /// </summary>
        private static void OnCurrentUICultureChanged(System.EventArgs e)
        {
            if (CurrentUICultureChanged != null)
            {
                CurrentUICultureChanged(null, e);
            }
        }
    }
}
